<template>
	<view style="background-color: #FFFFFF;">
		<view style="background-color: #FFFFFF;height: calc( 100vh - 280rpx); overflow-y: auto;padding-bottom: 40rpx;">
		<view class="tline" style="background-color: #F6FAFA;"></view>
		<view class="pl25 pr25">
			<view class="pt30 pb30 flex flex-ac flex-pj bb" @click="toRecord">
				<text style="font-size: 30rpx;">咨询人</text>
				<view class="flex flex-ac" v-if="personalFileId==0">
					<text class="ft12" style="font-size: 30rpx;color: #B0B0B0;">请选择咨询人</text>
					<image class="arrow ml15" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/arrow.png"></image>
				</view>
				<view class="flex flex-ac" v-if="personalFileId!=0">
					<text class="ft12" style="font-size: 30rpx;">{{consultant}}</text>
					<image class="arrow ml15" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/arrow.png"></image>
				</view>
			</view>
			<view class="pt30 pb30 flex flex-ac flex-pj bb" @click="toAddress">
				<text style="font-size: 30rpx;">地址</text>
				<view class="flex flex-ac" v-if="adressInfo==0">
					<text class="it1 ftc ft12" style="font-size: 30rpx;color: #B0B0B0;">我们会根据您的位置为你匹配药房</text>
					<text class="ft12" style="font-size: 30rpx;color: #B0B0B0;">请选择地址</text>
					<image class="arrow ml15" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/arrow.png"></image>
				</view>

				<view class="flex flex-ac" v-if="adressInfo!=0">
					<text class="ft12" style="font-size: 30rpx;">{{adressInfo}}</text>
					<image class="arrow ml15" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/arrow.png"></image>
				</view>

			</view>
			<view class="pt30 pb30 flex flex-ac flex-pj bb" @click="openaim">
				<text style="font-size: 30rpx;">问诊目的</text>
				<view class="flex flex-ac">
					<text v-if="aimkey==1" style="font-size: 30rpx;">在线复诊</text>
					<text v-else-if="aimkey==2" style="font-size: 30rpx;">复诊续方</text>
					<text v-else class="ft12" style="font-size: 30rpx;color: #B0B0B0;">请输入您的问诊目的</text>
					<image class="arrow ml15" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/arrow.png"></image>
				</view>
			</view>
			<view class="pt30 pb30 flex flex-ac flex-pj bb" v-if="aimkey==1" @click="toMedcalRecords">
				<text style="font-size: 30rpx;">就诊记录</text>
				<view class="flex flex-ac" v-if="medcalRecords==0">
					<text class="ft12" style="font-size: 30rpx;color: #B0B0B0;">请选择需要复诊的就诊记录</text>
					<image class="arrow ml15" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/arrow.png"></image>
				</view>
				<view class="flex flex-ac" v-if="medcalRecords!=0">
					<text class="ft12" style="font-size: 30rpx;">已选择就诊档案</text>
					<image class="arrow ml15" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/arrow.png"></image>
				</view>
			</view>
			
			<view class="pt30 pb30 flex flex-ac flex-pj bb" v-if="aimkey==2" @click="toChufangRecords">
				<text style="font-size: 30rpx;">历史处方</text>
				<view class="flex flex-ac" v-if="chufangRecords==0">
					<text class="ft12" style="font-size: 30rpx;color: #B0B0B0;">请选择需要复诊的历史处方</text>
					<image class="arrow ml15" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/arrow.png"></image>
				</view>
				<view class="flex flex-ac" v-if="chufangRecords!=0">
					<text class="ft12" style="font-size: 30rpx;">已选择历史处方</text>
					<image class="arrow ml15" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/arrow.png"></image>
				</view>
			</view>
			
			<view class="pt30 pb30 flex flex-ac flex-pj bb" @click="toWenZRecords">
				<text style="font-size: 30rpx;">中医问诊单</text>
				<view class="flex flex-ac" v-if="problems.length==0">
					<text class="ft12" style="font-size: 30rpx;color: #B0B0B0;">请填写问诊单帮助医生更好判断</text>
					<image class="arrow ml15" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/arrow.png"></image>
				</view>
				<view class="flex flex-ac" v-if="problems.length>0">
					<text class="ft12" style="font-size: 30rpx;">已填写中医问诊单</text>
					<image class="arrow ml15" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/arrow.png"></image>
				</view>
			</view>
		</view>
		<view class="tline" style="background-color: #F6FAFA;"></view>
		<view class="pl25 pr25">
			<view class="pt30 pb30">症状描述</view>
			<!-- <textarea @input="setKeyword" v-if="keyword==0" class="block w100 pad20 box" maxlength="150" placeholder="请输入症状描述(150字)"></textarea> -->
			<textarea @input="setKeyword"  class="block w100 pad20 box" maxlength="150"></textarea>
			<view class="pt30 pb30">
				舌苔、舌底照片
				<!-- <text class="ft12 c999" style="margin-left: 10rpx;">根据病情需要上传，系统加密仅医生可见</text> -->
			</view>
			<view class="pt30 pb30" style="padding-top: 0rpx;flex-direction: column;display: flex;">
				<text class="ft12 c999" style="margin-left: 10rpx;">建议用后置摄像头在自然光下拍摄，请关闭美颜</text>
				<text class="ft12 c999" style="margin-left: 10rpx;">拍舌苔照，长大嘴巴，上下伸长舌头</text>
				<text class="ft12 c999" style="margin-left: 10rpx;">拍舌舌底照，尽力向上抬起舌头</text>
			</view>
			<view class="imgs pl10 pr10" style="align-items: center;flex-wrap: wrap;">
				<block v-for="item in Imgs" :key="item">
					<view style="position: relative;">
						<image :src="item" style="height: 186rpx;width: 186rpx;" mode="scaleToFill"></image>
						<image src="../static/imgs/xx.png" 
						style="height: 42rpx;width: 42rpx;border-radius: 50%;
							position: absolute;top:-10rpx;right: -10rpx;" @click="delImg(item)"></image>	
					</view>
				</block>
				<view v-if="Imgs.length<8" @click="uploadImg" style="width: 190rpx;height: 186rpx;background: #EDF6F5;border-radius: 8rpx;display: flex;flex-direction: column;align-items: center;justify-content: center;">
					<image src="../../static/images/pzp.png" style="width: 50%;" mode="widthFix"></image>
					<view>拍照</view>
				</view>
			</view>
		</view>
		</view>
		<view style="width: 100%;position: fixed;bottom: 40rpx;">
			<view class="add-btn" @click="insertMessegOrder">向医生问诊</view>
			<view class="pt20 pb20 flex flex-ac ft11" style="padding-left: 40rpx;margin-top: 20rpx;">
				<!-- <text class="check2n box" v-if="!state" @click="saveState"></text>
				<image class="check2y" v-else @click="state=!state" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/check2y.png"></image>
				 -->
				 <radio-group @click="state=!state">
				 	<radio value="1" :checked="state" color="#2A82E4"/>
				</radio-group>
				<text class="c999 ml15" style="margin-left: 0rpx;font-weight: bolder;">同意</text>
				<text class="c74a6fd ml5" @click="selectHopBook" style="color: #2A82E4;">邛崃民众门诊部患者知情同意书</text>
			</view>
		</view>
		<!-- 选择问诊目的 -->
		<view class="win flex flex-ac flex-pc" v-if="aimstate">
			<view class="ml25 mr25 w100 bgfff brund10">
				<view class="ft16 pt30 pb30 ftc">请选择问诊目的</view>
				<view class="rela aim">
					<view class="pt30 pb30 bb flex flex-ac flex-pj" @click.stop="aimkey=1">
						<view class="it1">
							<view class="ft16 bold">在线复诊</view>
							<view class="ft12 c999 mt20">针对复诊患者病情情况给出诊疗建议（可开具处方）</view>
						</view>
						<!-- <image v-if="aimkey==1" class="ycheck ml25" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/check2y.png"></image>
						<view v-else class="ncheck box ml25"></view> -->
						<!-- <radio-group @click="state=!state"> -->
						 	<radio value="1" :checked="aimkey==1" style="transform:scale(0.8);color: #2A82E4;" color="#2A82E4"/>
						<!-- </radio-group> -->
					</view>
					<view class="pt30 pb30 bb flex flex-ac flex-pj" @click.stop="aimkey=2">
						<view class="it1">
							<view class="ft16 bold">复诊续方</view>
							<view class="ft12 c999 mt20">针对复诊患者线上复诊以及再次复购药品</view>
						</view>
						<!-- <image v-if="aimkey==2" class="ycheck ml25" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/check2y.png"></image>
						<view v-else class="ncheck box ml25"></view> -->
						<radio value="1" :checked="aimkey==2" style="transform:scale(0.8);color: #2A82E4;" color="#2A82E4"/>
					</view>
					<view class="flex btnaum pt15 pb15 ft16">
						<text class="it1 ftc" @click="cancelaim">取消</text>
						<text class="c999 line"></text>
						<text class="it1 ftc c74a6fd" @click="confirmaim" style="color: #2A82E4;">确定</text>
					</view>
				</view>
			</view>
		</view>
		

		
		<view v-if='isSaveing' style="position: fixed;height: 100%;
			width: 100%;background-color: rgba(8, 8, 7, 0.5);z-index: 999;top:0rpx;left: 0rpx;display: flex;align-items: center;justify-content: center;">
			<icon type="waiting" size="26"/>
			<text style="color: #FFFFFF;margin-left: 20rpx;font-size: 38rpx;">提交中...</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				wx: "https://app.mzmzb.com",//https://app.mzmzb.com   http://localhost:6110
				isNum: 2, //2:图文
				usrUserId: null,
				personalFileId: 0, //档案ID
				medDocInfoId: null, //医生ID
				consultant: null, //咨询人姓名
				
				age: null, //咨询人姓名
				
				sex: null, //咨询人性别
				messagefee: null, //图文咨询费用
				adressInfo: 0, //收货地址
				medcalRecords: 0, //流水号
				
				chufangRecords: 0, //处方
				problems: '',
				answer: '',
				keyword: 0,
				state: false,
				current: 0,
				winstate: false,
				value: '',
				aimstate: false,
				aimkey: null,
				Imgs: [],
				imgPaths: '',
				from:'',
				isSaveing: false ,// 是否正在提交订单
				
				docUserId: null,
				docPhoto: null,
				docname: null,
			};
		},
		onLoad(option) {
			var that = this;
			this.from = option.from;
			this.usrUserId = uni.getStorageSync('usrUserId')
			// that.medDocInfoId = uni.getStorageSync('medDocInfoId')
			
			this.docUserId = option.docUserId
			this.medDocInfoId = option.medDocInfoId
			this.docPhoto = option.docPhoto
			this.docname = option.docname
			
			that.messagefee = uni.getStorageSync('messagefee')
			
			that.aimkey = option.aimkey;
			that.keyword = uni.getStorageSync('keyInfo');
			that.state = uni.getStorageSync('state');
			
			uni.setStorageSync("isNum", that.isNum)
			uni.setStorageSync("adressInfo", that.adressInfo)
			uni.setStorageSync("medcalRecords", that.medcalRecords)
			uni.setStorageSync("aimkey", that.aimkey)
			
			this.getUserPersonalId()
		},
		methods: {
			getUserPersonalId(){
				uni.request({
					url: this.wx + '/application-usrapp/wx/getUserPersonalId.tz',
					method:'GET',
					header: {
						'content-type': 'application/json'
					},
					data:{
						userId: this.usrUserId,
						type:1
					},
					success:(res)=>{
						this.personalFileId = res.data.result.id
						this.consultant = res.data.result.name
						this.sex = res.data.result.sex
						this.age = res.data.result.age/12
					}
				})
			},
			tohome(){
				uni.reLaunch({
					url: '/pages/index/index'
				})
			},
			//选择咨询人
			toRecord() {
				var that = this;
				uni.navigateTo({
					url: '/pages/record/index?userId=' + that.usrUserId + '&medDocInfoId=' + that.medDocInfoId
				})
			},
			//跳转到收货地址首页
			toAddress() {
				uni.navigateTo({
					url: '/pages/address/index?userId=' + this.usrUserId + '&medDocInfoId=' + this.medDocInfoId
				})
			},
			toTitle() {
				uni.showToast({
					title: '请选择咨询人',
					icon: 'error'
				})
			},
			//跳转就诊记录首页
			toMedcalRecords() {
				//console.log('consultant===',this.consultant)
				if(this.consultant){
					var that = this;
					uni.navigateTo({
						url: '/pages2/jiuzjl/index?userId=' + that.usrUserId + '&medDocInfoId=' + that.medDocInfoId +
							'&consultant=' + that.consultant + '&personalFileId=' + that.personalFileId + '&videofee=' + that.messagefee +
							'&adressInfo=' + that.adressInfo + '&aimkey=' + that.aimkey + '&isNum=' + that.isNum
					})
				}else{
					uni.showToast({
						title: '请选择咨询人',
						icon: 'error'
					})
				}
			},
			toChufangRecords() {
				if(this.consultant){
					uni.navigateTo({
						url: '/pages3/chufang/index?userId=' + this.usrUserId + '&medDocInfoId=' 
							+ this.medDocInfoId+'&usrPersonalId='+ this.personalFileId
							+'&isChinOrder=1'
					})
				}else{
					uni.showToast({
						title: '请选择咨询人',
						icon: 'error'
					})
				}
			},
			toWenZRecords(){
				if(this.consultant){
					uni.navigateTo({
						url: '/pages5/wenzd/index?sex='+this.sex
					})
				}else{
					uni.showToast({
						title: '请选择咨询人',
						icon: 'error'
					})
				}
			},
			//获取症状描述输入的值
			setKeyword(e) {
				this.keyword = e.detail.value
				console.log(this.keyword)
				uni.setStorage({
					key: 'keyInfo',
					data: this.keyword,
				})
			},
			delImg(val){
				this.Imgs = this.Imgs.filter(item=>item!=val)
			},
			uploadImg() {
				var that = this
				uni.chooseImage({
					count: 9, // 默认9
					sizeType: ['original', 'camera'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: function(res) {
						uni.showLoading({
							title: '上传中...'
						});
						// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
						var tempFilePaths = res.tempFilePaths
						var myDate = new Date()
						var ossPath = 'ueditor/upload/weixin/' + myDate.getFullYear()
						var fileKeylist = []
						for (var i = 0; i < tempFilePaths.length; i++) {
							// 获取文件后缀
							//var pathArr = tempFilePaths[i].split('.')
							const imgsrc = res.tempFilePaths[i];
							const index = imgsrc.lastIndexOf(".");
							const imgExtension = imgsrc.substring(index + 1, imgsrc.length);
							console.log("后缀：" + imgExtension)
							//  随机生成文件名称
							var fileRandName = Date.now() + "" + parseInt(Math.random() * 1000)
							var fileName = fileRandName + '.' + imgExtension
							// 要提交的key
							var fileKey = ossPath + '/' + fileName
							fileKeylist.push(fileKey)
							uni.uploadFile({
								url: 'https://tuozhen1.oss-cn-beijing.aliyuncs.com/',
								filePath: tempFilePaths[i],
								name: 'file',
								formData: {
									name: tempFilePaths[i],
									key: fileKey,
									policy: 'eyJleHBpcmF0aW9uIjoiMjMzNy0wNC0xM1QwMjo0MjoxMi40ODFaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCJ1ZWRpdG9yL3VwbG9hZC93ZWl4aW4vIl1dfQ==',
									OSSAccessKeyId: 'Te5npXFe8IdIxPgQ',
									signature: 'I1Dv3FioQ4hoBh0ui2Rvsax+fAI=',
									success_action_status: "200"
								},
								success: function(res) {
									console.log(res)
								}
							})
						}

						setTimeout(function() {
							that.getImgs(fileKeylist);
						}, 5000);
					}
				})
			},
			getImgs(e) {
				var that = this;
				var imgLists = []
				if (uni.getStorageSync('imgs').length != 0) {
					for (var j = 0; j < uni.getStorageSync('imgs').length; j++) {
						imgLists.push(uni.getStorageSync('imgs')[j])
					}
				}
				for (var i = 0; i < e.length; i++) {
					var upliadImages = "http://tuozhen1.oss-cn-beijing.aliyuncs.com/" + e[i];
					console.log("携带的参数：" + upliadImages)
					imgLists.push(upliadImages);
				}
				uni.hideLoading()
				that.Imgs = imgLists
				uni.setStorageSync('imgs', imgLists)
			},
			openwin() {
				this.winstate = !this.winstate;
			},
			setDate(e) {
				let state = e.currentTarget.dataset.state;
				if (state == 'before' && this.current > 0) {
					this.current = this.current - 1;
				} else if (state == 'after' && this.current < 2) {
					this.current = this.current + 1;
				}
			},
			choseTime(e) {
				this.value = e.currentTarget.dataset.value;
				this.openwin();
			},
			openaim() {
				this.aimstate = !this.aimstate;
			},
			confirmaim() {
				this.openaim();
			},
			cancelaim() {
				this.aimkey = null;
				this.openaim();
			},
			saveState() {
				var that = this
				that.state = !that.state;
				uni.setStorage({
					key: 'state',
					data: that.state
				})
			},
			selectHopBook() {
				uni.navigateTo({
					url: '/pages/agreement/index'
				})
			},
			//提交图文咨询
			insertMessegOrder() {
				if(this.isSaveing){
					return
				}
				let aimkey = null
				this.isSaveing = true
				var that = this;
				if (that.state == true) {
					if (that.personalFileId > 0) {
						if(this.Imgs.length < 1){
							uni.showToast({
								title: '请上传舌苔照片',
								icon: 'error'
							})
							that.isSaveing = false
							return
						}
						if (that.adressInfo == 'null' || that.adressInfo == 'undefined' || that.adressInfo == null || that.adressInfo ==
							0) {
							uni.showToast({
								title: '请选择地址',
								icon: 'error'
							})
							that.isSaveing = false
							return
						}
						if (that.aimkey == 'null' || that.aimkey == 'undefined' || that.aimkey == null) {
							uni.showToast({
								title: '请选择问诊目的',
								icon: 'error'
							})
							that.isSaveing = false
							return
						}
				
						if (that.keyword == 'null' || that.keyword == 'undefined' || that.keyword == null || that.keyword == 0) {
							uni.showToast({
								title: '请输入症状描述',
								icon: 'error'
							})
							that.isSaveing = false
							return
						}
						
						if (!this.problems || this.problems.length == 0) {
							uni.showToast({
								title: '请填写中医问诊单',
								icon: 'error'
							})
							that.isSaveing = false
							return
						}
						
						if(that.aimkey==2 && (that.chufangRecords == 'null'
							|| that.chufangRecords == 'undefined' || that.chufangRecords == null || that.chufangRecords == 0)){
							uni.showToast({
								title: '请选择历史处方',
								icon: 'error'
							})
							that.isSaveing = false
							return
						}
						
						if (that.medcalRecords == 'null' || that.medcalRecords == 'undefined' || that.medcalRecords == null || that.medcalRecords ==
							0) {
							that.medcalRecords = 0
						}
						console.log("medcalRecords:" + that.medcalRecords)
						console.log("price:" + that.messagefee)
						console.log("medDocInfoId:" + that.medDocInfoId)
						console.log("usrPersonalFileId:" + that.personalFileId)
						console.log("address:" + that.adressInfo)
						console.log("problem:" + that.keyword)
						console.log("usrUserId:" + that.usrUserId)
						console.log("image:" + that.Imgs)
						
						if(that.aimkey==1){
							aimkey = 0
						}
						if(that.aimkey==2){
							aimkey = 1
						}
						// console.log("aimkey"+that.aimkey)
						if (that.medcalRecords == 0) {
							uni.request({
								url: that.wx + '/application-usrapp/chinMed/saveChinMedOrd.tz?usrUserId=' + that.usrUserId + '&price=' +
									that.messagefee + '&medDocInfoId=' + that.medDocInfoId +
									'&usrPersonalFileId=' + that.personalFileId + '&address=' + that.adressInfo + '&problem=' + that
									.keyword + '&image=' + that.Imgs+'&isContinuation='+aimkey+'&problems='+this.problems+'&answer='+this.answer+'&historyRecipeId='+that.chufangRecords,
								method: 'POST',
								header: {
									'content-type': 'application/x-www-form-urlencoded',
									'originateenum': 'WEIXIN'
								},
								success:(res) =>{
									console.log(res.data);
									if (res.data.success == true) {
										var price = res.data.result.price;
										if(price!=0){
											var orderId = res.data.result.id;
											uni.login({
												success:(loginRes) =>{
													uni.request({
														url: that.wx+'/application-usrapp/wx/messegPay.tz?money=' + price + '&orderId=' +
															orderId + '&code=' + loginRes.code,
														header: {
															'content-type': 'application/x-www-form-urlencoded',
														},
														method: "POST",
														success: (item) =>{
															if (item.data.success) {
																var timestamp = item.data.result.timeStamp;
																var nonceStr = item.data.result.nonceStr;
																var packages = 'prepay_id=' + item.data.result.prepayid;
																var paySign = item.data.result.sign;
																uni.requestPayment({
																	timeStamp: timestamp,
																	nonceStr: nonceStr,
																	package: packages,
																	signType: 'MD5',
																	paySign: paySign,
																	success: (res) =>{
																		that.isSaveing = false
																		that.sendMsg(orderId)
																		that.joinChat(orderId)
																		// uni.navigateTo({
																		// 	url: '/pages5/advisory/index?userId=' + that.usrUserId+'&ispay=1'
																		// })
																	},
																	fail: (err) =>{
																		that.isSaveing = false
																		console.log('fail:' + JSON.stringify(err));
																	}
																});
															} else {
																that.isSaveing = false
																that.warnMessage = item.data.errMsg;
																that.warnTime = 3000;
																that.$refs.popup.open();
															}
														},
														fail(error) {
															that.isSaveing = false
															that.warnMessage = '支付失败';
															that.warnType = 'error';
															that.$refs.popup.open();
														},
													})
												}
											});
										}
										if(price==0){
											var orderId = res.data.result.id;
											uni.request({
												url: that.wx+'/application-usrapp/pay/tzPay.tz?money=' + price + '&remark=' +
													orderId + '&usrGetTypeEnum=user_word_expenditure'+'&usrUserId='+that.usrUserId+'&thirdPayEnum=TzPay',
												header: {
													'content-type': 'application/x-www-form-urlencoded',
												},
												method: "POST",
												success: (item) =>{
													if (item.data.success) {
														that.isSaveing = false
														that.sendMsg(orderId)
														that.joinChat(orderId)
														// uni.navigateTo({
														// 	url:'/pages5/advisory/index?userId=' + that.usrUserId+'&ispay=1'
														// })
													}
												},
											})
										}
									}
								}
							})
						} else {
							that.medcalRecords = uni.getStorageSync("medcalRecordId")
							console.log("流水号3："+that.medcalRecords)
							uni.request({
								url: that.wx + '/application-usrapp/chinMed/saveChinMedOrd.tz?usrUserId=' + that.usrUserId +
									'&medcalRecords=' +
									that.medcalRecords + '&price=' + that.messagefee + '&medDocInfoId=' + that.medDocInfoId +
									'&usrPersonalFileId=' + that.personalFileId + '&address=' + that.adressInfo + '&problem=' + that
									.keyword + '&image=' + that.Imgs+'&isContinuation='+aimkey+'&problems='+this.problems+'&answer='+this.answer,
								method: 'POST',
								header: {
									'content-type': 'application/x-www-form-urlencoded',
									'originateenum': 'WEIXIN'
								},
								success: (res)=>{
									console.log(res.data);
									if(res.data.code=="99999"){
										that.isSaveing = false
										uni.showToast({
											title: res.data.errMsg
										})
									}
									if (res.data.success == true) {
										var price = res.data.result.price;
										if(price!=0){
											var orderId = res.data.result.id;
											uni.login({
												success: (loginRes)=>{
													uni.request({
														url: that.wx+'/application-usrapp/wx/messegPay.tz?money=' + price + '&orderId=' +
															orderId + '&code=' + loginRes.code,
														header: {
															'content-type': 'application/x-www-form-urlencoded',
														},
														method: "POST",
														success: (item) =>{
															if (item.data.success) {
																var timestamp = item.data.result.timeStamp;
																var nonceStr = item.data.result.nonceStr;
																var packages = 'prepay_id=' + item.data.result.prepayid;
																var paySign = item.data.result.sign;
																uni.requestPayment({
																	timeStamp: timestamp,
																	nonceStr: nonceStr,
																	package: packages,
																	signType: 'MD5',
																	paySign: paySign,
																	success: (res) =>{
																		that.isSaveing = false
																		that.sendMsg(orderId)
																		that.joinChat(orderId)
																		// uni.navigateTo({
																		// 	url: '/pages5/advisory/index?userId=' + that.usrUserId+'&ispay=1'
																		// })
																	},
																	fail:(err) =>{
																		console.log('fail:' + JSON.stringify(err));
																	}
																});
															} else {
																that.isSaveing = false
																that.warnMessage = item.data.errMsg;
																that.warnTime = 3000;
																that.$refs.popup.open();
															}
														},
														fail(error) {
															that.isSaveing = false
															that.warnMessage = '支付失败';
															that.warnType = 'error';
															that.$refs.popup.open();
														},
													})
												}
											});
										}
										if(price==0){
											var orderId = res.data.result.id;
											uni.request({
												url: that.wx+'/application-usrapp/pay/tzPay.tz?money=' + price + '&remark=' +
													orderId + '&usrGetTypeEnum=user_word_expenditure'+'&usrUserId='+that.usrUserId+'&thirdPayEnum=TzPay',
												header: {
													'content-type': 'application/x-www-form-urlencoded',
												},
												method: "POST",
												success: (item) =>{
													if (item.data.success) {
														that.isSaveing = false
														that.sendMsg(orderId)
														that.joinChat(orderId)
														// uni.navigateTo({
														// 	url:'/pages5/advisory/index?userId=' + that.usrUserId+'&ispay=1'
														// })
													}
												},
											})
										}
									}
								}
							})
						}
					} else {
						that.isSaveing = false
						uni.showToast({
							title: '请选择咨询人',
							icon: 'error'
						})
					}
				
				} else {
					that.isSaveing = false
					uni.showToast({
						title: '请仔细阅读邛崃民众门诊部患者知情同意书并勾选',
						icon: 'error'
					})
				}
				
			},
			
			sendMsg(orderId){
				let avatarUrl = uni.getStorageSync("avatarUrl");
				let nickName = uni.getStorageSync("nickName");
				let from = uni.getStorageSync("usrUserId")
				let id = this.$im.conn.getUniqueId();
				let msg = new this.$im.message('txt', id);
				
				let extObj = {
					patientInfo: `${this.consultant} ${this.sex} ${this.age}`,
					problem:this.keyword,
					tcmQuestion:false,
					orderid:orderId,
					user_nickname:nickName,
					user_head:avatarUrl,
					user_id:from,
					messageType: 'txt',
					doctor_nickname: this.docname,
					doctor_id: this.medDocInfoId,
					doctor_head:this.docPhoto
				}
				
				let msgStr = ''
				if(this.aimkey == 2 ){
					msgStr = '[请求续方]'
					extObj.selectRecipeId = this.chufangRecords
				}else{
					msgStr = '[问诊单]'
				}
				
				if(this.Imgs && this.Imgs.length>0){
					extObj.images = this.Imgs.join(',')
				}
				
				msg.set({
					to: this.docUserId,
					from: from,
					msg: msgStr,
					roomType: false,
					chatType: 'singleChat',
					ext:extObj,
					success(){
						console.log('问诊通知成功了')
					},
					fail(){
						console.log('问诊通知失败了')
					}
				});
				this.$im.conn.send(msg.body);
			},
			//加入聊天
			joinChat(orderId) {
				let userId = uni.getStorageSync("usrUserId")
				let avatarUrl = uni.getStorageSync("avatarUrl");
				let nickName = uni.getStorageSync("nickName");
				var orderExt = {}; //发消息所需要的拓展数据
				orderExt.orderId = orderId;
				orderExt.messageType = '图文订单';
				orderExt.doctorId = this.docUserId;
				orderExt.doctorHead = this.docPhoto;
				orderExt.doctorNickname = this.docname;
				orderExt.userId = userId;
				orderExt.userHead = avatarUrl;
				orderExt.userNickname = nickName;
				orderExt.ordStatus = 1
				uni.redirectTo({
					url: '/pages/chatroom/chatroom?username=' + JSON.stringify(orderExt)
				});
			}
			
		}
	};
</script>

<style lang="less">
	.add-btn{
		margin: auto;
		height: 88rpx;
		line-height: 88rpx;
		width: 90%;
		background: linear-gradient(90deg, #F1BAB2 0%, #F78D7D 100%);
		box-shadow: 0px 20rpx 25rpx 0px rgba(247,141,125,0.4);
		border-radius: 44rpx;
		color: #FFFFFF;
		text-align: center;
	}
	.win {
		position: fixed;
		width: 100%;
		height: 100%;
		left: 0;
		top: 0;
		z-index: 100;
		background: rgba(0, 0, 0, 0.3);

		swiper {
			height: 450rpx;

			.arrow {
				width: 24rpx;
				height: 40rpx;
			}

			.date {
				line-height: 40rpx;
			}

			.arrowl {
				width: 10%;
				left: 0;
				padding-left: 15%;
				z-index: 100;
			}

			.arrowr {
				width: 10%;
				right: 0;
				padding-right: 15%;
				z-index: 100;
			}

			.time text {
				float: left;
				display: inline-block;
				width: 33.33%;
				line-height: 90rpx;
			}
		}

		.aim {
			padding: 0 50rpx;

			.ncheck {
				display: inline-block;
				width: 40rpx;
				height: 40rpx;
				border: 1rpx solid #EFEFEF;
			}

			.ycheck {
				width: 40rpx;
				height: 40rpx;
			}

			.btnaum {
				.line {
					width: 1rpx;
					background: #EFEFEF;
				}

				text {
					display: inline-block;
					height: 100rpx;
					line-height: 100rpx;
				}
			}
		}
	}

	textarea {
		height: 200rpx;
		border-radius: 5rpx;
		border: 1rpx solid #efefef;
	}

	.check2n {
		display: inline-block;
		width: 34rpx;
		height: 34rpx;
		border: 2rpx solid #838383;
	}

	.check2y {
		width: 24rpx;
		height: 24rpx;
	}

	.bottomBtn {
		margin-top: 30rpx;
		position: relative;
		height: 125rpx;
		width: 100%;

		.bottomBtncon {
			position: fixed;
			width: 700rpx;
			z-index: 10;
			left: 25rpx;
			height: 125rpx;
			bottom: 0;
			background: #FFFFFF;

			.btns {
				width: 660rpx;
				height: 100rpx;
				line-height: 100rpx;
				border-radius: 10rpx;
				background: #74a6fd;
			}
		}
	}

	.imgs {
		display: flex;
		image {
			width: 120rpx;
			height: 97rpx;
			margin: 10rpx;
		}
	}
</style>
